import _ from 'lodash';
/* eslint no-useless-escape:0 import/prefer-default-export:0 */

const categories = [
  'dynamic_total_price',
  'dynamic_sale_price',
  'dynamic_stock_price',
  'dynamic_plan_price',
  'dynamic_un_send_price',
  'dynamic_right_price',
  'land_invest_price',
  'land_hk_price',
  'un_start_price',
  'un_reach_price',
  'un_get_card_price',
  'on_way_price',
  'stock_90_price',
  'stock_90_180_price',
  'stock_180_360_price',
  'stock_360_720_price',
  'stock_720_price',
  'stock_exist_room_price',
];

export const buildRowStructure = list => {
  const rows = [];
  list.forEach(value => {
    if (
      _.filter(rows, x => x.orgId === value.orgId).length === 0 &&
      value.groupCode === 'dynamic_total_price'
    ) {
      const initSum = {};
      categories.forEach(category => {
        initSum[category] = {};
      });
      rows.push({
        id: value.orgId,
        ...initSum,
        ...value,
      });
    }
  });
  list.forEach(value => {
    const items = _.filter(rows, x => x.orgId === value.orgId);
    if (items && items.length > 0) {
      const row = items[0];
      row[value.groupCode] = {
        ...value,
        roomTotalPrice: value.roomTotalPrice ? _.toNumber(value.roomTotalPrice) : 0, // 房间金额
        carPlateTotalPrice: value.carPlateTotalPrice ? _.toNumber(value.carPlateTotalPrice) : 0, // 车位金额
        roomCount: value.roomCount ? _.toNumber(value.roomCount) : 0, // 房源套数
        carPlateCount: value.carPlateCount ? _.toNumber(value.carPlateCount) : 0, // 车位套数
        rightPrice: value.rightPrice ? _.toNumber(value.rightPrice) : 0, // 权益货值
        area: value.area ? _.toNumber(value.area) : 0, // 面积
        // area: value.area ? _.toNumber(value.area) : 0, // 均价
      };
    }
  });

  // _.each(rows, val => {
  //   _.each(categories, x => {
  //     if (Object.keys(val[x]).length <= 0) {
  //       delete val[x];
  //     }
  //   })
  // })
  return rows;
};

export const hasChildren = (list, item) => {
  const items = _.filter(list, element => element.parentOrgId === item.orgId);
  return items.length > 0;
};
export const buildTree = (list, parentOrgId = '0') => {
  const tree = [];
  list.forEach(value => {
    const x = value;
    if (parentOrgId !== '0' ? `${x.parentOrgId}` === parentOrgId : x.orgType === 'org_project') {
      if (hasChildren(list, x)) {
        delete x.isLeaf;
        x.children = [];
        x.children = buildTree(list, x.orgId);
      } else {
        delete x.children;
        x.isLeaf = true;
      }
      tree.push(x);
    }
  });
  return tree;
};
export const getExpandedRowKeys = list => {
  const expandedRowKeys = [];
  _.forEach(list, value => {
    expandedRowKeys.push(value.orgId);
  });
  return expandedRowKeys;
};
